package org.hbhk.hxl.gateway.services;

import org.hbhk.hxl.gateway.redis.RedisUtil;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class HmsTopicListener implements ApplicationRunner {

	private Logger log = LoggerFactory.getLogger(getClass());

	private String topic = "hmsgateway";

	@Autowired
	private IGatewayService gatewayService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		RedissonClient redissonClient = RedisUtil.getRedissonManager();
		if (redissonClient == null) {
			return;
		}
		RTopic rTopic = redissonClient.getTopic(topic);
		rTopic.addListener(String.class, new MessageListener<String>() {
			@Override
			public void onMessage(CharSequence channel, String msg) {
				// 重置gateway数据
				log.info("开始刷新gateway数据:" + msg);
				gatewayService.reset(msg);

			}
		});
	}

}
